package com.yonyou.jrmob.contacts.schedule.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.schedule.vo.ContactsVO;
import com.yonyou.jrmob.contacts.schedule.vo.SelectContactsVO;

import java.util.List;

/**
 * Created by Administrator on 2016/7/11.
 */
public class SelectDptAdapter extends BaseAdapter {

    private Context context;
    private List<ContactsVO> list;
    private List<SelectContactsVO> selectList;

    public SelectDptAdapter(Context context, List<ContactsVO> list, List<SelectContactsVO> selectList) {
        this.context = context;
        this.list = list;
        this.selectList = selectList;
    }
    /**
     * @param list
     */
    public void updateListView(List<ContactsVO> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ContactsVO getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        viewholder holder = null;
        final ContactsVO mContent = list.get(position);
        if (convertView == null) {
            holder = new viewholder();
            convertView = View.inflate(context,
                    R.layout.contactperson_item, null);
            holder.layout_aa = (LinearLayout) convertView
                    .findViewById(R.id.layout_aa);
            holder.point_quan = (TextView) convertView
                    .findViewById(R.id.point_quan);
            holder.name_quan = (TextView) convertView
                    .findViewById(R.id.name_quan);
            holder.checkbox_quan = (CheckBox) convertView
                    .findViewById(R.id.checkbox_quan);
            holder.pk_quan = (TextView) convertView
                    .findViewById(R.id.pk_quan);
            holder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            convertView.setTag(holder);
        } else {
            holder = (viewholder) convertView.getTag();
        }

        int section = getSectionForPosition(position);

        if (position == getPositionForSection(section)) {
            holder.tvLetter.setVisibility(View.VISIBLE);
            holder.tvLetter.setText(mContent.getSortLetters());
        } else {
            holder.tvLetter.setVisibility(View.GONE);
        }

        for(int i = 0; i<selectList.size();i++){
            if(mContent.getCuserId().equals(selectList.get(i).getCuserId())){
                mContent.setIscheck(true);
            }
        }

        holder.checkbox_quan.setTag(list.get(position).getCuserId());
        holder.checkbox_quan
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        //Toast.makeText(context, isChecked+"", Toast.LENGTH_SHORT).show();
                        updateData(buttonView.getTag() + "", isChecked);

                        for(int i = 0; i<selectList.size();i++){
                            if(buttonView.getTag().equals(selectList.get(i).getCuserId())){
                                selectList.get(i).setIschange(true);
                            }
                        }

                    }
                });


        holder.layout_aa.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, dip2px(55)));
        holder.layout_aa.setBackgroundColor(convertView.getResources().getColor(
                R.color.transparent));

        holder.point_quan.setVisibility(View.VISIBLE);
        holder.checkbox_quan.setEnabled(true);
        holder.checkbox_quan.setVisibility(View.VISIBLE);
        if (position % 5 == 0) {
            holder.point_quan.setBackground(convertView.getResources().getDrawable(
                    R.drawable.shape_red)); // 前面的图片
        } else if (position % 5 == 1) {
            holder.point_quan.setBackground(convertView.getResources().getDrawable(
                    R.drawable.shape_blue_normal)); // 前面的图片
        } else if (position % 5 == 2) {
            holder.point_quan.setBackground(convertView.getResources().getDrawable(
                    R.drawable.shape_00)); // 前面的图片
        } else if (position % 5 == 3) {
            holder.point_quan.setBackground(convertView.getResources().getDrawable(
                    R.drawable.shape_1l)); // 前面的图片
        } else if (position % 5 == 4) {
            holder.point_quan.setBackground(convertView.getResources().getDrawable(
                    R.drawable.shape_black_0f)); // 前面的图片
        }
        holder.checkbox_quan.setChecked(list.get(position).getIscheck()); // 是否勾选

        String name = "";
        if (null != list.get(position)
                && list.get(position).getContactName().trim().length() > 2) {
            name = list
                    .get(position)
                    .getContactName()
                    .substring(
                            list.get(position).getContactName().trim().length() - 2).trim();
        } else {
            name = list.get(position).getContactName().trim();
        }
        holder.point_quan.setText(name);
        holder.name_quan.setText(list.get(position).getContactName().trim()); // 姓名
        holder.pk_quan.setText(position + ""); // pk

        return convertView;
    }

    public int dip2px(float dpValue) {
        final float scale = context.getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    void updateData(String pk, boolean isChecked) {
        for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCuserId().equals(pk)) {
                list.get(i).setIscheck(isChecked);
			}
		}
    }

    /**
     *
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     *
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    class viewholder {
        LinearLayout layout_aa;
        TextView point_quan;
        TextView name_quan;
        TextView pk_quan;
        CheckBox checkbox_quan;
        TextView tvLetter;
    }

}
